package com.alks.common.aspect;

import com.alks.common.aop.AutoPageAop;
import com.alks.entity.data.page.PageParam;
import com.alks.entity.data.page.PageRecord;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class PageParamAspect {


    private Boolean autoPageFlag = false;

    private Page pageInfo;

    @Pointcut("@annotation(autoPage)")
    public void pageParamPointcut(AutoPageAop autoPage) {}

    /**
     * 自动分页
     * @param joinPoint 切入点
     * @param autoPage 注解
     */
    @Before("pageParamPointcut(autoPage)")
    public void beforeHandlePageParam(JoinPoint joinPoint, AutoPageAop autoPage) {
        Object[] args = joinPoint.getArgs();
        // 处理分页参数
        for (Object arg : args) {
            if (arg instanceof PageParam) {
                PageParam request = (PageParam) arg;
                boolean sizeFlag = request.getPageSize() == null || (request.getPageSize() != -1 && request.getPageSize() <= 0);
                boolean numFlag = request.getPageNum() == null || (request.getPageNum() != -1 && request.getPageNum() <= 0);
                request.setPageSize(sizeFlag ? 10 : request.getPageSize());
                request.setPageNum(numFlag ? 1 : request.getPageNum());
                if (!request.getPageSize().equals(-1) && !request.getPageNum().equals(-1)) {
                    PageHelper.startPage(request.getPageNum(), request.getPageSize(),autoPage.count());
                    pageInfo = PageHelper.getLocalPage();
                    autoPageFlag = true;
                }
                break;
            }
        }
    }

    /**
     * 自动获取求和
     * @param joinPoint 分页
     * @param autoPage 注解
     * @param result 返回值
     */
    @AfterReturning(pointcut = "pageParamPointcut(autoPage)", returning = "result", argNames = "joinPoint,autoPage,result")
    public void afterHandlePageParam(JoinPoint joinPoint, AutoPageAop autoPage, Object result) {
        // 需要总数，返回值是PageRecord，已经进行过PageStart 则设置总数
        if (autoPage.count() && result instanceof PageRecord && autoPageFlag) {
            PageRecord<?> record = (PageRecord<?>) result;
            record.setTotal(pageInfo.getTotal());
        }
    }

//    @Around("pageParamPointcut(autoPage)")
//    public Object handlePageParam(ProceedingJoinPoint joinPoint, AutoPageAop autoPage) throws Throwable {
//        Object[] args = joinPoint.getArgs();
//        boolean start = false;
//        Page<?> pageInfo = null;
//        // 处理分页参数
//        for (Object arg : args) {
//            if (arg instanceof PageParam) {
//                PageParam request = (PageParam) arg;
//                boolean sizeFlag = request.getPageSize() == null || (request.getPageSize() != -1 && request.getPageSize() <= 0);
//                boolean numFlag = request.getPageNum() == null || (request.getPageNum() != -1 && request.getPageNum() <= 0);
//                request.setPageSize(sizeFlag ? 10 : request.getPageSize());
//                request.setPageNum(numFlag ? 1 : request.getPageNum());
//                if (!request.getPageSize().equals(-1) && !request.getPageNum().equals(-1)) {
//                    PageHelper.startPage(request.getPageNum(), request.getPageSize(), autoPage.count());
//                    start = true;
//                    // 保存当前的分页信息
//                    pageInfo = PageHelper.getLocalPage();
//                }
//                break;
//            }
//        }
//        // 执行方法
//        Object result = joinPoint.proceed();
//        // 如果返回值是PageRecord，则设置总数
//        if (autoPage.count() && result instanceof PageRecord && start) {
//            PageRecord pageRecord = (PageRecord) result;
//            if (pageInfo != null) {
//                pageRecord.setTotal(pageInfo.getTotal());
//            }
//        }
//        // 清理PageHelper的ThreadLocal
//        PageHelper.clearPage();
//        return result;
//    }
}